package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.ApCollectionBehaviorService;
import com.heima.common.constants.ArticleBehaviorConstants;
import com.heima.common.exception.CustomException;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.behavior.dtos.CollectionBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ClassName: ApLikeBehaviorServiceImpl
 * Description:
 * date: 2023/8/27 0027 16:33
 *
 * @author zHan
 */
@Service
@Slf4j
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private ApCollectionMapper apCollectionMapper;

    /**
     * 文章收藏
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult collectionBehavior(CollectionBehaviorDto dto) {
        // 检查参数
        checkParam(dto);
        // 获取当前用户
        ApUser user = AppThreadLocalUtil.getUser();
        // 收藏的key
        String collectionKey = ArticleBehaviorConstants.COLLECTION + user.getId()
                + ":" + dto.getType() + ":" + dto.getEntryId();
        // 如果为0，收藏
        if (dto.getOperation() == 0) {
            String value = JSON.toJSONString(dto);
            cacheService.set(collectionKey, value);
            // 对ap_collection表进行添加
            ApCollection apCollection = new ApCollection();
            // 拷贝对象
            BeanUtils.copyProperties(dto, apCollection);
            // 补全对象
            apCollection.setEntryId(user.getId());
            apCollection.setCollectionTime(new Date());
            apCollection.setArticleId(dto.getEntryId());
            // 保存数据
            apCollectionMapper.insert(apCollection);
        } else {
            // 删除收藏
            cacheService.delete(collectionKey);
            // 删除数据
            apCollectionMapper.delete(Wrappers.<ApCollection>lambdaQuery()
                    .eq(ApCollection::getEntryId, user.getId())
                    .eq(ApCollection::getArticleId, dto.getEntryId()));
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 检查参数
     *
     * @param dto
     */
    private void checkParam(CollectionBehaviorDto dto) {
        if (dto.getEntryId() == null || dto.getOperation() == null || dto.getType() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getType() < 0 || dto.getType() > 1) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getOperation() < 0 || dto.getOperation() > 1) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getPublishedTime() == null) {
            dto.setPublishedTime(new Date());
        }
    }
}
